/**
 * 2017年5月3日
 */
package util.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import org.lab.libsvm.svm_node;

import pojo.dto.services.DocumentVector;

/**
 * 
 * 转化类
 * 把这样的
 * c1,MjM5ODI0NDIyNQ==4004535832 (\t)	-15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * MjM5ODI0NDIyNQ==4004535832	(\t)  -15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * c2,MjM5ODI0NDIyNQ==4004535832 (\t)	-15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * 
 * 转化成
 * 1 -15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * -1 -15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * 2 (\t) -15.418162580986973 5.533133398625068 -26.24123669725668 6.15946473547956 -4.134956639260054 -2.8917397495388286 -13.711052137776278 8.181440686874339 13.362585380120436 -13.580482888180995 -1.0104949924279936 -5.892026193672791 2.859514665979077 4.020788782596355 11.649008407007386 0.40734980213892413 8.274161520646885 -0.3429704940645024 -8.413447984930826 3.754761514683196 -4.1332425411601434 -2.7258808271726593 3.5922176672611386 -3.5430008680632454 7.448214798932895
 * 根据有无类标,进行不同的处理...
 * 
 * @author Alex
 *
 */
public class DocumentIDVectorReader {
	final File src;
	Vector<svm_node[]> vx;
	Vector<Double> vy;
	public DocumentIDVectorReader(File src) {
		this.src = src;
	}
	public /*Iterator<DocumentVector>*/void read() throws FileNotFoundException, IOException{
		try(BufferedReader fp = new BufferedReader(new FileReader(this.src))){
			
			vy = new Vector<Double>();
			vx = new Vector<svm_node[]>();
			while(true){
				String line = fp.readLine();
				if(line == null) break;
				boolean hasLabel = line.charAt(0) == '$';
				//如果不是按\t分割的,则找第一个空格
				boolean isTabSep = line.indexOf("\t") != -1;
				String label ;
				String vec;
				String pieces [] = null;
				if(isTabSep){
					pieces = line.split("\t");
				}else{
					pieces = line.split(" ");
				}
				if(hasLabel){
					label = pieces[0].trim();
					vec = pieces[2].trim();
					vy.addElement(new Double(label.split(",")[0].substring(1)));
				}else{
					vy.add(new Double(-1));
					vec = pieces[1].trim();
				}
				String vecVal[] = vec.split(" ");
				svm_node[] x = new svm_node[vecVal.length];
				for(int i =0;i<vecVal.length;i++){
					x[i] = new svm_node();
					x[i].index = i;
					x[i].value = Double.parseDouble(vecVal[i]);
				}
				vx.addElement(x);
			}
		}
	}
	/**
	 * @return the vx
	 */
	public Vector<svm_node[]> getVx() {
		return vx;
	}
	/**
	 * @param vx the vx to set
	 */
	public void setVx(Vector<svm_node[]> vx) {
		this.vx = vx;
	}
	/**
	 * @return the vy
	 */
	public Vector<Double> getVy() {
		return vy;
	}
	/**
	 * @param vy the vy to set
	 */
	public void setVy(Vector<Double> vy) {
		this.vy = vy;
	}
	
	
	
	
}
